package cn.lhl.web.controller.company;

import cn.lhl.domain.company.Company;
import cn.lhl.service.company.CompanyService;
import cn.lhl.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.UUID;

@Controller
@RequestMapping(value = "/company")
public class CompanyController extends BaseController {

    @Autowired
    private CompanyService companyService;

    //private HttpServletRequest request;

    /*@RequestMapping(value = "/list", name = "企业列表查询")
    public String list() {
        List<Company> list = companyService.findAll();
        request.setAttribute("list", list);
        //return "/WEB-INF/pages/company/company-list.jsp";
        return "company/company-list";
    }*/

    /**
     * 分页查询企业列表
     * page： 代表当前点击的页码
     * size： 代表当前页里的行数
     */
   /* @RequestMapping(value = "/list", name = "企业信息分页")
    public String list(@RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "5") int size) {
        //传递参数调用service
        PageBean pageBean = companyService.findPageBean(page, size);
        model.addAttribute("page", pageBean);
        return "company/company-list";
    }*/

    /**
     * 根据pageHelper进行分页
     *
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/list", name = "企业列表查询")
    public String list(@RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "5") int size) {

        //PageInfo:是PageHelper封装的PageBean
        PageInfo pageInfo = companyService.findByPageHelper(page, size);
        request.setAttribute("page", pageInfo);

        return "company/company-list";
    }


    @RequestMapping(value = "/toAdd", name = "去新增企业表单")
    public String toAdd() {
        return "company/company-add";
    }

    @RequestMapping(value = "/edit", name = "企业信息")
    public String edit(Company company) {
        if (company.getId() == null) {
            company.setId(UUID.randomUUID().toString());
            companyService.save(company);
        } else {
            companyService.update(company);
        }
        return "redirect:/company/list.do";
    }

    @RequestMapping(value = "/toUpdate", name = "跳转企业编辑页面")
    public String toUpdate(String id) {
        Company company = companyService.findById(id);
        model.addAttribute("company", company);
        return "/company/company-update";
    }


    @RequestMapping(value = "/delete", name = "删除企业信息")
    public String delete(String id) {
        companyService.delete(id);
        return "redirect:/company/list.do";
    }

}

